class ExampleSpider(scrapy.Spider):
    name = 'liebiao'
    allowed_domains = ['liebiao.com']
    headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US;\
                 rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
    start_urls = ['http://anshun.liebiao.com/', 'http://anqing.liebiao.com/']
    #start_requests 默认遍历start_urls
    def start_requests():
        pass
   
    def parse(self, response):
        for each in response.selector.css('.xb-box-1>dl>dd>ul>li>a ::attr(href)').extract():
            yield scrapy.Request(url= str(response.url)[:-1] + each, callback=self.get_url)
    
    def get_url(self,response):
        for each in response.selector.css('.fang-title ::attr(href)').extract():
            yield scrapy.Request(url=each,callback=self.detail)
        if response.selector.css('.next a'):
            next_url = response.selector.css('.next a ::attr(href)').extract()[0]
            yield scrapy.Request(url=next_url,callback=self.get_url)

    def detail(self,response):
        item = LiebiaoItem()
        item['name'] = response.selector.css('.name ::text').extract()[0].strip()
        tel = response.selector.css('.lxr-phone .phone-size ::text').extract()[0]
        item['tel'] = tel
        # item['addr'] = ''.join(response.selector.css('.detail-right>a ::text').extract())
        prefix_addr = response.selector.css('.weizhi ::text').extract()[0]
        item['addr'] = ''.join(response.selector.css('.detail-content dl:last-child .detail-right a ::text').extract()) + prefix_addr
        # 调用查询归属地接口
        label_url = "http://www.ip138.com:8080/search.asp?action=mobile&mobile=" + tel.strip()
        wb_data = requests.get(label_url,headers=self.headers)
        wb_data.encoding = 'gb2312'
        item['belongtowhere'] = ''.join(Selector(response=wb_data).css('.tdc2 ::text').extract()[4:5]).replace(r'\ax0','')
        yield item